﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Threading.Tasks;
using Ession;
using System.Net;

namespace LoginWebUsingWebBrowser
{
    public partial class frmProxy : Form
    {
        public frmProxy()
        {
            InitializeComponent();
            VThreadCount = 10;
        }

        List<string> l = new List<string>();

        public void SetProxy()
        {
            this.BeginInvoke((Action)(
                                              () =>
                                              {
                                                  foreach (var proxy in l)
                                                  {
                                                      rtbrichTextBox1.Text += proxy + "\r\n";
                                                  }
                                              }
                                          ));
        }
        public List<string> ProxyList
        {
            get { return l; }
            set
            {
                l = value;

            }
        }
        private void btnbutton1_Click(object sender, EventArgs e)
        {

        }

        List<string> ValidProxyList = new List<string>();

        private void btnbutton2_Click(object sender, EventArgs e)
        {
            if (File.Exists("proxySeekUrls.txt"))
            {
                File.Create("proxySeekUrls.txt");
            }
            Process.Start("Notepad.exe", "proxySeekUrls.txt");
        }

        private string validUrl;
        private string validResult;
        Ession.HttpRequestSender ap = new HttpRequestSender();
        public int VThreadCount { get; set; }
        private int count;
        private void btnbutton3_Click(object sender, EventArgs e)
        {
            validUrl = txttextBox1.Text;
            validResult = textBox1.Text;
            Task.Factory.StartNew(
                () =>
                {
                    foreach (var proxy1 in ProxyList)
                    {

                        try
                        {
                            WebProxy wp = new WebProxy(proxy1);
                            var result = ap.GetHtml(validUrl, Encoding.GetEncoding("GB2312"), 5000, wp);

                            if (result.Contains(validResult))
                            {
                                ValidProxyList.Add(proxy1);
                            }
                        }
                        catch (Exception)
                        {
                            continue;

                        }

                        this.BeginInvoke((Action)(
                                      () =>
                                      {
                                          count++;
                                          rtbrichTextBox1.Text += "证验证" + count + "个代理\r\n";


                                      }
                                  ));
                    }
                    this.BeginInvoke((Action)(
                                          () =>
                                          {
                                              rtbrichTextBox1.Text += "过滤后================\r\n";
                                              foreach (var proxy in ValidProxyList)
                                              {
                                                  rtbrichTextBox1.Text += proxy + "\r\n";
                                              }
                                          }
                                      ));


                }
                );
        }
    }
}
